package com.newegg.datafeed.adserving.mapper;

import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.newegg.datafeed.adserving.domain.Company;

@Mapper
public interface CompanyMapper {
	
	@Select("SELECT id,company_name,account,nickname,password,email,phone,address,corporate_entity_name,taxid,verify,verify_by,disable,memo,create_date,update_date "
			+ "FROM company where account=#{account} AND password=#{password} and verify=true and disable=false")
    @Results({
    	@Result(property = "companyName",  column = "company_name"),
    	@Result(property = "corporateEntityName",  column = "corporate_entity_name"),
    	@Result(property = "verifyBy",  column = "verify_by"),
    	@Result(property = "createDate",  column = "create_date"),
    	@Result(property = "updateDate",  column = "update_date")
    })
	Company login(@Param("account") String account,@Param("password") String password);
	
	@Select("SELECT id,company_name,account,nickname,password,email,phone,address,corporate_entity_name,taxid,verify,verify_by,disable,memo,create_date,update_date "
			+ "FROM company where id=#{id}")
    @Results({
    	@Result(property = "companyName",  column = "company_name"),
    	@Result(property = "corporateEntityName",  column = "corporate_entity_name"),
    	@Result(property = "verifyBy",  column = "verify_by"),
    	@Result(property = "createDate",  column = "create_date"),
    	@Result(property = "updateDate",  column = "update_date")
    })
	Company get(@Param("id") Integer id);

	@Select("SELECT id,company_name,account,nickname,password,email,phone,address,corporate_entity_name,taxid,verify,verify_by,disable,memo,create_date,update_date FROM company")
    @Results({
    	@Result(property = "companyName",  column = "company_name"),
    	@Result(property = "corporateEntityName",  column = "corporate_entity_name"),
    	@Result(property = "verifyBy",  column = "verify_by"),
    	@Result(property = "createDate",  column = "create_date"),
    	@Result(property = "updateDate",  column = "update_date")
    })
	List<Company> getAll();
	
	@Select("SELECT id FROM company where account=#{user}")
	List<String> checkName(@Param("user") String user);
	
	@Insert("INSERT INTO company(company_name,account,nickname,password,email,phone,address,corporate_entity_name,taxid,verify,verify_by,disable,memo,create_date,update_date) VALUES (#{companyName},#{account},#{nickname},#{password},#{email},#{phone},#{address},#{corporateEntityName},#{taxid},#{verify},#{verifyBy},#{disable},#{memo},#{createDate},#{updateDate});")
	@Options(useGeneratedKeys=true, keyProperty="id")
	void insert(Company company);
	
	@Update("UPDATE company SET company_name=#{companyName},account=#{account},nickname=#{nickname},password=#{password},email=#{email},phone=#{phone},address=#{address},corporate_entity_name=#{corporateEntityName},taxid=#{taxid},verify=#{verify},verify_by=#{verifyBy},disable=#{disable},memo=#{memo},create_date=#{createDate},update_date=#{updateDate} WHERE id=#{id}")
    void update(Company company);
	
	@Delete("DELETE FROM company WHERE id=#{id}")
    void delete(int id);

	@Select("SELECT count(id) FROM company")
	Long count();

	@Select("SELECT id,company_name,account,nickname,password,email,phone,address,corporate_entity_name,taxid,verify,verify_by,disable,memo,create_date,update_date "
			+ "FROM company LIMIT #{start},#{page}")
    @Results({
    	@Result(property = "companyName",  column = "company_name"),
    	@Result(property = "corporateEntityName",  column = "corporate_entity_name"),
    	@Result(property = "verifyBy",  column = "verify_by"),
    	@Result(property = "createDate",  column = "create_date"),
    	@Result(property = "updateDate",  column = "update_date")
    })
	List<Company> page(@Param("start") int start,@Param("page") int page);
}
